var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
var Schema = mongoose.Schema;

var CategorySchema = new Schema({
    // 分类名称
    name: {type: String, required: true},
    // URL名称
    slug: {type: String, required: true},
    // 分类创建时间
    created_at: {type: Date, default: Date.now}
});

// 静态方法 - 根据分类名查找分类信息
CategorySchema.statics.getCategory = function (slug, cb) {
  this.findOne({'slug': slug}, cb);
};

// 获取所有分类
CategorySchema.statics.getCategories = function (cb) {
  this.find({}, cb);
};

// 静态方法 - 根据分类id查找分类信息
CategorySchema.statics.getCategoryById = function (id, cb) {
  this.find({'_id': id}, cb);
};

// 生成Model
module.exports = mongoose.model('Category', CategorySchema);
